package net.hn.hnms.biz.safety.trinity.service;

import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.safety.trinity.model.TreeViolationsManagementVo;
import net.hn.hnms.biz.api.safety.trinity.model.ViolationRecordDto;
import net.hn.hnms.biz.api.safety.trinity.model.ViolationRecordVo;
import net.hn.hnms.biz.common.jpa.utils.ResponsePage;
import net.hn.hnms.biz.safety.trinity.dao.WorkSafetyViolationDao;
import net.hn.hnms.biz.safety.trinity.entity.OriginalViolationRecordVo;
import net.hn.hnms.biz.safety.trinity.entity.WorkSafetyViolation;
import net.hn.hnms.biz.safety.trinity.until.Dictionaries;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 智呈物联 nzw
 * @date 2022/8/5
 */
@Service
@RequiredArgsConstructor
public class WorkSafetyViolationService {

    private final JdbcTemplate locationJdbcTemplate;

    WorkSafetyViolationDao workSafetyViolationDao;

    EntityManager entityManager;

    @Autowired
    public void setWorkSafetyViolationDao(WorkSafetyViolationDao workSafetyViolationDao) {
        this.workSafetyViolationDao = workSafetyViolationDao;
    }

    @Autowired
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public TreeViolationsManagementVo threeViolations() {
        TreeViolationsManagementVo treeViolationsManagementVo = new TreeViolationsManagementVo();
        //三违总数
        treeViolationsManagementVo.setViolationCount(workSafetyViolationDao.countAll());

        List<Map<String, Object>> list = workSafetyViolationDao.threeViolations();
        for (Map map : list) {
            if (map.get("violation_level") != null) {
                String violationLevel = map.get("violation_level") + "";
                if (violationLevel.equals("0001")) {
                    treeViolationsManagementVo.setSerious(Integer.parseInt(map.get("count") + ""));
                    continue;
                }
                if (violationLevel.equals("0002")) {
                    treeViolationsManagementVo.setLarger(Integer.parseInt(map.get("count") + ""));
                    continue;
                }
                if (violationLevel.equals("0003")) {
                    treeViolationsManagementVo.setGenerally(Integer.parseInt(map.get("count") + ""));
                    continue;
                }
            } else {
                continue;
            }
        }
        return treeViolationsManagementVo;
    }

    public ResponsePage<ViolationRecordVo> page(PageQuery pageQuery, ViolationRecordDto violationRecordDto, String mineCode) {
        WorkSafetyViolation workSafetyViolation = new WorkSafetyViolation();

        //分页参数赋值
        workSafetyViolation.setViolationType(violationRecordDto.getType());
        workSafetyViolation.setViolationLevel(violationRecordDto.getLevel());

        String startDay = violationRecordDto.getStartDay();
        String endDay = violationRecordDto.getEndDay();

        String sql = "select * from work_safety_violation where 1=1";
        String countSql = "select count(*) from work_safety_violation where 1=1";
        String where = "";
        if (startDay!=null&&!"".equals(startDay)) {
            where += " and data_time>= '" + startDay+"'";
        }
        if (endDay!=null&&!"".equals(endDay)) {
            where += " and data_time <= '" + endDay+"'";
        }
        if(StringUtils.isNotBlank(violationRecordDto.getType())){
            where += " and violation_type = '" + violationRecordDto.getType()+"' ";
        }
        if(StringUtils.isNotBlank(violationRecordDto.getLevel())){
            where += " and violation_level = '" + violationRecordDto.getLevel()+"' ";
        }
        where += " and trim(mine_code) = '" + mineCode + "'";
        Integer pageSize = pageQuery.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }
        Integer pageNum = pageQuery.getPageNum();
        if (pageNum == null ) {
            pageNum = 0;
        }
        String page = " LIMIT " + pageSize + " OFFSET " + (pageNum-1)*pageSize;
//        System.out.println("sql::::"+sql + where + page);
        List<WorkSafetyViolation> workSafetyViolationList = locationJdbcTemplate.query(sql + where + page, BeanPropertyRowMapper.newInstance(WorkSafetyViolation.class));
        Map<String, Object> map = locationJdbcTemplate.queryForMap(countSql + where);
        List<ViolationRecordVo> list = new ArrayList<>();
        for (WorkSafetyViolation workSafety : workSafetyViolationList) {
            ViolationRecordVo violationRecordVo = new ViolationRecordVo();
            //违章人员名称
            violationRecordVo.setName(workSafety.getViolationPersonName());
            //三违等级
            if (Dictionaries.GAOTOUYAOMINECODE.equals(mineCode)) {
                violationRecordVo.setLevel(getGaoTouYaoLevelName(workSafety.getViolationLevel()));
            } else {
                violationRecordVo.setLevel(workSafety.getViolationLevel());
            }
            //违章情况
            violationRecordVo.setSituation(workSafety.getViolationReason());
            //违章类别
            if (Dictionaries.GAOTOUYAOMINECODE.equals(mineCode)) {
                violationRecordVo.setType(getGaoTouYaoTypeName(workSafety.getViolationType()));
            } else {
                violationRecordVo.setType(workSafety.getViolationType());
            }
            //违章日期
            violationRecordVo.setDay(workSafety.getViolationDate());
            list.add(violationRecordVo);
        }

        ResponsePage<ViolationRecordVo> violationRecordVoResponsePage = new ResponsePage<>(list, Integer.parseInt(map.get("count") + ""));
        return violationRecordVoResponsePage;
    }

    private String getGaoTouYaoLevelName(String levelCode) {
        if (StringUtils.isBlank(levelCode)) return null;
        switch (levelCode) {
            case "001":
                return "一般";
            case "002":
                return "轻微";
            case "003":
                return "严重";
            case "004":
                return "特别严重";
        }
        return "";
    }

    private String getGaoTouYaoTypeName(String typeCode) {
        if (StringUtils.isBlank(typeCode)) return null;
        switch (typeCode) {
            case "001":
                return "通用类";
            case "002":
                return "机电类";
            case "003":
                return "运输类";
            case "004":
                return "车辆运输类";
            case "005":
                return "采煤类";
            case "006":
                return "掘进类";
            case "007":
                return "一通三防类";
            case "008":
                return "探放水类";
            case "009":
                return "装置类";

        }
        return "";
    }

    public ResponsePage<OriginalViolationRecordVo> originalPage(PageQuery pageQuery, ViolationRecordDto violationRecordDto, String mineCode){
        Integer pageSize = pageQuery.getPageSize();
        Integer pageNum = pageQuery.getPageNum();
        if (pageSize==null){
            pageSize=10;
        }
        if (pageNum==null){
            pageNum=1;
        }

        String page = " order by hx_create_time desc LIMIT "+pageSize+" OFFSET "+(pageNum-1)*pageSize;
        String sql = "select * from hx_c_sanweixinxi";
        String count = "select count(*) from hx_c_sanweixinxi";
        StringBuffer where = new StringBuffer(" where 1=1 ");

        String fxr = violationRecordDto.getFxr();
        if (fxr!=null&&!"".equals(fxr)){
            where.append(" and hx_fxr = '"+fxr+"' ");
        }
        String wzrszdw = violationRecordDto.getWzrszdw();
        if (wzrszdw!=null&&!"".equals(wzrszdw)){
            where.append(" and hx_wzrszdw = '"+wzrszdw+"' ");
        }
        String startDay = violationRecordDto.getStartDay();
        if (startDay!=null&&!"".equals(startDay)){
            where.append(" and TO_DATE(hx_fssj,'yyyy-MM-dd') >= '"+startDay+"' ");
        }
        String endDay = violationRecordDto.getEndDay();
        if (endDay!=null&&!"".equals(endDay)){
            where.append(" and TO_DATE(hx_fssj,'yyyy-MM-dd') <= '"+endDay+"' ");
        }
        String wzr = violationRecordDto.getWzr();
        if (wzr!=null&&!"".equals(wzr)){
            where.append(" and hx_wzr = '"+wzr+"' ");
        }
        String level = violationRecordDto.getLevel();
        if (level!=null&&!"".equals(level)){
            where.append(" and hx_level = '"+level+"' ");
        }
        String type = violationRecordDto.getType();
        if (type!=null&&!"".equals(type)){
            where.append(" and hx_classify = '"+type+"' ");
        }
        List<Map<String,Object>> stationInfo = locationJdbcTemplate.queryForList(sql+where.toString()+page);
        Map<String,Object> pageMap = locationJdbcTemplate.queryForMap(count+where.toString());

        List<OriginalViolationRecordVo> data = new ArrayList<>();
        stationInfo.forEach(map -> {
            OriginalViolationRecordVo originalViolationRecordVo = new OriginalViolationRecordVo();
            originalViolationRecordVo.setFssj(map.get("hx_fssj")+"");
            originalViolationRecordVo.setBc(map.get("hx_bc")+"");
            originalViolationRecordVo.setFsdd(map.get("hx_fsdd")+"");
            originalViolationRecordVo.setWzrszdw(map.get("hx_wzrszdw")+"");
            originalViolationRecordVo.setWzr(map.get("hx_wzr")+"");
            originalViolationRecordVo.setClassify(map.get("hx_classify")+"");
            originalViolationRecordVo.setBaqhwms(map.get("hx_hwss")+"");
            originalViolationRecordVo.setLevel(map.get("hx_level")+"");
            originalViolationRecordVo.setKhbzy(map.get("hx_fkje")+"");
            originalViolationRecordVo.setFkje(map.get("hx_fkje")+"");
            originalViolationRecordVo.setFxr(map.get("hx_fxr")+"");
            originalViolationRecordVo.setCreateusername(map.get("hx_create_user_name")+"");
            originalViolationRecordVo.setCreateorgname(map.get("hx_create_org_name")+"");
            data.add(originalViolationRecordVo);
        });
        return new ResponsePage<OriginalViolationRecordVo>(data,Integer.parseInt(pageMap.get("count")+""));
    }
}
